//
//  MeatMarketSectionView.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#define selfH 37

#import "MeatBuySectionView.h"

@interface MeatBuySectionView ()


/**
 BuyMeat SectionView's DiamondIcon - >UIImageView
 */
@property (nonatomic, strong) UIImageView *diamondIcon;


/**
 BuyMeat SectionView's RecommendedShopIcon - >UIImageView

 */
@property (nonatomic, strong) UIImageView *recommendedShopIcon;


/**
 BuyMeat SectionView's CartBtn - >UIButton
 */
@property (nonatomic, strong) UIButton *cartBtn;


/**
 BuyMeat SectionView's SwitchBtn - >UIButton
 */
@property (nonatomic, strong) UIButton *switchBtn;


/**
 BuyMeat SectionView's ScreeningBtn - >UIButton
 */
@property (nonatomic, strong) UIButton *screeningBtn;

@end

@implementation MeatBuySectionView

#pragma mark - lifecycle methods

- (void)cancelScreeningNotification{
    
    self.screeningBtn.selected = NO;
    [self.screeningBtn setImage:[UIImage imageNamed:@"meat_buy_screen_gray"] forState:UIControlStateNormal];
}


/**
 这个方法相当于vc中的viewDidLoad
 */
- (void)didMoveToWindow {
    if (self.window) {
        [kNotificationCenter addObserver:self selector:@selector(cancelScreeningNotification) name:kMeatMarketSwitchNotification object:nil];
    }
}


/**
 从当前window删除 相当于-viewDidUnload
 */
- (void)willMoveToWindow:(UIWindow *)newWindow {
    if (newWindow == nil) {
        //移除通知
        [kNotificationCenter removeObserver:self name:kMeatMarketSwitchNotification object:nil];
    }
}


- (void)setupUI{
    
    [self addSubview:self.diamondIcon];
    [self addSubview:self.recommendedShopIcon];
    [self addSubview:self.cartBtn];
    [self addSubview:self.switchBtn];
    [self addSubview:self.screeningBtn];
}


- (void)setupLayout{
    
    self.height = selfH;

    
    {
        _diamondIcon.size = CGSizeMake(15, 15);
        _diamondIcon.mj_x = 18;
        _diamondIcon.mj_y = (self.height - _diamondIcon.height)/2.0;
    }
    
    
    {
        _recommendedShopIcon.size = CGSizeMake(63, 15);
        _recommendedShopIcon.mj_y = _diamondIcon.mj_y;
        _recommendedShopIcon.mj_x = _diamondIcon.right + 6;
    }
    
    
    {
        _screeningBtn.size = CGSizeMake(selfH, selfH);
        _screeningBtn.mj_y = (self.height - _screeningBtn.height)/2.0;
        _screeningBtn.mj_x = self.right - _screeningBtn.width - 18;
    }
    
    
    {
        _switchBtn.size = CGSizeMake(selfH, selfH);
        _switchBtn.mj_y = _screeningBtn.mj_y;
        _switchBtn.mj_x = _screeningBtn.left - _switchBtn.width - 13;
    }
    
    
    {
        _cartBtn.size = CGSizeMake(selfH, selfH);
        _cartBtn.mj_y = _screeningBtn.mj_y;
        _cartBtn.mj_x = _switchBtn.left - _cartBtn.width - 13;
    }
    
}


#pragma mark - setter and getter

- (UIImageView *)diamondIcon{
    if (!_diamondIcon) {
        _diamondIcon = [UIImageView makeImageByImageName:@"meat_buy_start" cornerRadius:0];
    }
    return _diamondIcon;
}


- (UIImageView *)recommendedShopIcon{
    if (!_recommendedShopIcon) {
        _recommendedShopIcon = [UIImageView makeImageByImageName:@"meat_buy_recommendIcon" cornerRadius:0];
    }
    return _recommendedShopIcon;
}


- (UIButton *)cartBtn{
    
    if (!_cartBtn) {
        _cartBtn = [UIButton makeButtonWithImageName:@"meat_buy_cart"];
        
        kWeakSelf(self);
        _cartBtn.tapBlock = ^{
            if (weakself.dealJumpCartBlock) {
                weakself.dealJumpCartBlock();
            }
        };
    }
    return _cartBtn;
}


- (UIButton *)switchBtn{
    
    if (!_switchBtn) {
        _switchBtn = [UIButton makeButtonWithImageName:@"meat_buy_layout"];
        
        kWeakSelf(self);
        _switchBtn.tapBlock = ^{
            if (weakself.dealSwitchShopStyleBlock) {
                weakself.dealSwitchShopStyleBlock();
            }
        };
    }
    return _switchBtn;
}


- (UIButton *)screeningBtn{
    
    if (!_screeningBtn) {
        _screeningBtn = [UIButton makeButtonWithImageName:@"meat_buy_screen_gray"];
        _screeningBtn.selected = NO;
        
        kWeakSelf(self);
        _screeningBtn.tapBlock = ^{
            
            UIImage * image = nil;
            if (!weakself.screeningBtn.selected) {
                weakself.screeningBtn.selected = YES;
                image = [UIImage imageNamed:@"meat_buy_screen_green"];
            }else{
                weakself.screeningBtn.selected = NO;
                image = [UIImage imageNamed:@"meat_buy_screen_gray"];
            }
            
            [weakself.screeningBtn setImage:image forState:UIControlStateNormal];

            if (weakself.dealScreeningBlock) {
                weakself.dealScreeningBlock(weakself.screeningBtn);
            }
        };
    }
    return _screeningBtn;
}


@end
